﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class Pages_Login : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
            if (Session["Admin"] != null)
            {
                lbID.Text = Session["Admin"].ToString();
                munviLogin.ActiveViewIndex = 1;
                display(Session["Admin"].ToString());
            }
            else
            {
                if (Session["Manager"] != null)
                {
                    lbID.Text = Session["Manager"].ToString();
                    munviLogin.ActiveViewIndex = 1;
                    display(Session["Manager"].ToString());
                }
                else
                {
                    if (Session["Teacher"]!= null)
                    {
                        lbID.Text = Session["Teacher"].ToString();
                        munviLogin.ActiveViewIndex = 1;
                        display(Session["Teacher"].ToString());
                    }
                    else
                    {
                        if (Session["Student"]!= null)
                        {
                            lbID.Text = Session["Student"].ToString();
                            munviLogin.ActiveViewIndex = 1;
                            student(Session["Student"].ToString());
                        }
                        else
                        {
                            munviLogin.ActiveViewIndex = 0;
                        }
                    }
                }
            }
    }

    SqlConnection connLogin;


    protected void display(string user)
    {
        connLogin = new SqlConnection();
        connLogin.ConnectionString = ConfigurationManager.ConnectionStrings["Institte_of_Fine_ArtsConnectionString"].ConnectionString;
        connLogin.Open();
        string str = "select Firstname from Staff where StaffID ='"+user+"'";
        SqlCommand cmdDisplay = new SqlCommand(str, connLogin);
        SqlDataReader daDisplay = cmdDisplay.ExecuteReader();
        if (daDisplay.HasRows)
        {
            daDisplay.Read();
            lbStatusLogin.Text = daDisplay.GetString(0);
        }
    }

    protected void student(string user)
    {
        connLogin = new SqlConnection();
        connLogin.ConnectionString = ConfigurationManager.ConnectionStrings["Institte_of_Fine_ArtsConnectionString"].ConnectionString;
        connLogin.Open();
        string str = "select Firstname from Student where StudentID ='"+user+"'";
        SqlCommand cmdDisplay = new SqlCommand(str, connLogin);
        SqlDataReader daDisplay = cmdDisplay.ExecuteReader();
        if (daDisplay.HasRows)
        {
            daDisplay.Read();
            lbStatusLogin.Text = daDisplay.GetString(0);
        }
    }
         



    protected void mnLogin_ItemClick(object source, DevExpress.Web.ASPxMenu.MenuItemEventArgs e)
    {

        if (e.Item.Index.Equals(0))
        {
            Session["Admin"] = null;
            Session["Student"] = null;
            Session["Manager"] = null;
            Session["Teacher"] = null;
            Response.Redirect("~/Default.aspx");
        }
        else
        {
            if (lbID.Text == Session["Admin"])
            {
                Session["Admin"] = lbID.Text.ToString();
                Response.Redirect("~/Admin/ChangePassword.aspx");
            }
            else
            {
                if (lbID.Text == Session["Manager"])
                {
                    Session["Manager"] = lbID.Text.ToString(); ;
                    Response.Redirect("~/Manager/ChangePassword.aspx");
                }
                else
                {
                    if (lbID.Text == Session["Teacher"])
                    {
                        Session["Teacher"] = lbID.Text.ToString();
                        Response.Redirect("~/Teacher/ChangePassword.aspx");
                    }
                    else
                    {
                        if (lbID.Text == Session["Student"])
                        {
                            Session["Student"] = lbID.Text.ToString();
                            Response.Redirect("~/Student/ChangePassword.aspx");
                        }
                    }
                }
            }

        }
    }

    protected void btLogin_Click1(object sender, EventArgs e)
    {
        string user = txtUserLogin.Text;
        connLogin = new SqlConnection();
        connLogin.ConnectionString = ConfigurationManager.ConnectionStrings["Institte_of_Fine_ArtsConnectionString"].ConnectionString;
        connLogin.Open();

        if (txtUserLogin.Text == "")
        {
            lbErrorLogin.Text = "Username not null!";
        }
        else
        {
            if (txtPassLogin.Text == "")
            {
                lbErrorLogin.Text = "Password not null!";
            }
            else
            {
                if (txtUserLogin.Text == "Manager" || txtUserLogin.Text == "manager")
                {
                    SqlCommand cmdLogin = new SqlCommand("Select StaffId, Password from Staff where StaffID = '" + txtUserLogin.Text + "' and Password= '" + txtPassLogin.Text + "'", connLogin);
                    SqlDataReader daLogin = cmdLogin.ExecuteReader();
                    if (daLogin.HasRows)
                    {
                        daLogin.Read();
                        munviLogin.ActiveViewIndex = 1;
                        Session["Manager"] = user.ToString();
                        lbID.Text = "" + Session["Manager"] + "";
                        Response.Redirect("~/Manager/HomePage.aspx");
                    }
                    else
                    {
                        lbErrorLogin.Text = "Username or Password Invalid!!!";
                        munviLogin.ActiveViewIndex = 0;

                    }
                }
                else
                {
                    if (txtUserLogin.Text == "Administrator" || txtUserLogin.Text == "administrator" || txtUserLogin.Text == "Admin" || txtUserLogin.Text =="admin")
                    {
                        SqlCommand cmdLogin = new SqlCommand("Select StaffId, Password from Staff where StaffID = '" + txtUserLogin.Text + "' and Password= '" + txtPassLogin.Text + "'", connLogin);
                        SqlDataReader daLogin = cmdLogin.ExecuteReader();
                        if (daLogin.HasRows)
                        {
                            daLogin.Read();
                            munviLogin.ActiveViewIndex = 1;
                            Session["Admin"] = user.ToString();
                            lbID.Text = "" + Session["Admin"] + "";
                            Response.Redirect("~/Admin/HomePage.aspx");
                        }
                        else
                        {
                            lbErrorLogin.Text = "Username or Password Invalid!!!";
                            munviLogin.ActiveViewIndex = 0;

                        }
                    }
                    else
                    {
                        if (user[0] == 'T' || user[0] == 't')
                        {
                            SqlCommand cmdLogin = new SqlCommand("Select StaffId, Password from Staff where StaffID = '" + txtUserLogin.Text + "'and Password= '" + txtPassLogin.Text + "'", connLogin);
                            SqlDataReader daLogin = cmdLogin.ExecuteReader();
                            if (daLogin.HasRows)
                            {
                                daLogin.Read();
                                munviLogin.ActiveViewIndex = 1;
                                Session["Teacher"] = user.ToString();
                                lbID.Text = "" + Session["Teacher"] + "";
                                Response.Redirect("~/Teacher/HomePage.aspx");
                            }
                            else
                            {
                                lbErrorLogin.Text = "Username or Password Invalid!!!";
                                munviLogin.ActiveViewIndex = 0;

                            }
                        }
                        else
                        {
                            if (user[0] == 'S' || user[0] == 's')
                            {
                                SqlCommand cmdLogin = new SqlCommand("Select StudentID, Password from Student where StudentID = '" + txtUserLogin.Text + "'and Password = '" + txtPassLogin.Text + "'", connLogin);
                                SqlDataReader daLogin = cmdLogin.ExecuteReader();
                                if (daLogin.HasRows)
                                {
                                    daLogin.Read();
                                    //Session["FirstName"] = user.ToString();
                                    //lbStatusLogin.Text = "" + Session["FirstName"] + "";
                                    munviLogin.ActiveViewIndex = 1;
                                    Session["Student"] = user.ToString();
                                    lbID.Text = ""+Session["Student"]+"";
                                    Response.Redirect("~/Student/HomePage.aspx");
                                }
                                else
                                {
                                    lbErrorLogin.Text = "Username or Password Invalid!!!";
                                    munviLogin.ActiveViewIndex = 0;

                                }
                            }
                            else
                            {

                                lbErrorLogin.Text = "User not exist";
                                munviLogin.ActiveViewIndex = 0;
                            }
                        }
                    }
                }
            }
        }
    }
    protected void Cancel_Click(object sender, EventArgs e)
    {
        txtPassLogin.Text = "";
        txtUserLogin.Text = "";
        txtUserLogin.Focus();
    }
}